package com.tanhua.server.controller;

import com.tanhua.model.mongoDb.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-02-28 23:42
 * @Desc 动态Controller
 */
@RestController
@RequestMapping("/movements")
public class MovementsController {
    @Autowired
    private MovementsService movementsService;

    /**
     * 发布动态
     *
     * @param movement
     * @param imageContent
     * @return org.springframework.http.ResponseEntity
     * @Author Pan Rundong
     */
    @PostMapping
    public ResponseEntity publicMovements(Movement movement, MultipartFile[] imageContent) throws IOException {
        //1.调用service
        movementsService.publicMovements(movement, imageContent);
        //2.返回
        return ResponseEntity.ok(null);
    }

    /**
     * 查看动态
     *
     * @param page
     * @param pagesize
     * @param userId
     * @return org.springframework.http.ResponseEntity<com.tanhua.model.vo.MovementsVo>
     * @Author Pan Rundong
     */
    @GetMapping("/all")
    public ResponseEntity<PageResult> all(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pagesize,
                                          Long userId)
    {
        //1.调用service
        PageResult pageResult = movementsService.all(page, pagesize, userId);
        //2.返回
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查看好友动态
     *
     * @param page
     * @param pagesize
     * @return org.springframework.http.ResponseEntity<com.tanhua.model.vo.PageResult>
     * @Author Pan Rundong
     */
    @GetMapping
    public ResponseEntity<PageResult> friendsMovements(@RequestParam(defaultValue = "1") Integer page,
                                                       @RequestParam(defaultValue = "10") Integer pagesize)
    {
        //1.调用service
        PageResult pageResult = movementsService.friendsMovements(page, pagesize);
        //2.返回
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐动态
     *
     * @param page
     * @param pagesize
     * @return org.springframework.http.ResponseEntity<com.tanhua.model.vo.PageResult>
     * @Author Pan Rundong
     */
    @GetMapping("/recommend")
    public ResponseEntity<PageResult> recommend(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pagesize)
    {
        //1.调用service
        PageResult pageResult = movementsService.recommend(page, pagesize);
        //2.返回
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 根据动态Id查询动态
     *
     * @param movementId
     * @return org.springframework.http.ResponseEntity<com.tanhua.model.vo.MovementsVo>
     * @Author Pan Rundong
     */
    @GetMapping("/{id}")
    public ResponseEntity<MovementsVo> getById(@PathVariable("id") String movementId) {
        //1.调用service
        MovementsVo vo = movementsService.getById(movementId);
        //2.返回
        return ResponseEntity.ok(vo);
    }

    /**
     * 动态点赞
     *
     * @param
     * @return org.springframework.http.ResponseEntity<java.lang.Integer>
     * @Author Pan Rundong
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Integer> like(@PathVariable("id") String movementId) {
        //1.调用service
        Integer count = movementsService.like(movementId);
        //2.返回
        return ResponseEntity.ok(count);
    }

    /**
     * 取消点赞
     *
     * @param movementId
     * @return org.springframework.http.ResponseEntity<java.lang.Integer>
     * @Author Pan Rundong
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Integer> disLike(@PathVariable("id") String movementId) {
        //1.调用service
        Integer count = movementsService.disLike(movementId);
        //2.返回
        return ResponseEntity.ok(count);
    }

    /**
     * 动态喜欢
     *
     * @param movementId
     * @return org.springframework.http.ResponseEntity<java.lang.Integer>
     * @Author Pan Rundong
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Integer> love(@PathVariable("id") String movementId) {
        //1.调用service
        Integer count = movementsService.love(movementId);
        //2.返回
        return ResponseEntity.ok(count);
    }

    /**
     * 取消喜欢
     *
     * @param movementId
     * @return org.springframework.http.ResponseEntity<java.lang.Integer>
     * @Author Pan Rundong
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Integer> unLove(@PathVariable("id") String movementId) {
        //1.调用service
        Integer count = movementsService.unLove(movementId);
        //2.返回
        return ResponseEntity.ok(count);
    }

    /**
     * 谁看过我
     *
     * @param
     * @return org.springframework.http.ResponseEntity<java.util.List>
     * @Author Pan Rundong
     */
    @GetMapping("visitors")
    public ResponseEntity<List> visitors() {
        List<VisitorsVo> voList = movementsService.visitors();
        return ResponseEntity.ok(voList);
    }
}
